﻿using CJAtech.BBS.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Web.Helper;
using Bigger.Qiniu;

namespace CJAtech.BBS.Web.Controllers
{
    [BBSCorsFilter]
    [BBSConfigFilter("lms")]
    [SSOAuthFilter(BBSPowerType.CheckLogin)]
    public partial class LMSController : BBSBaseController
    {
        public override BBSBaseProcess CreateBBSProcess()
        {
            return new BBSBaseProcess("lms");
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult LoginOut()
        {
            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);
            string token = CookieHelper.GetCookieValue(CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");
            InvokeResult objInvokeResult = new InvokeResult();
            if (token.IsNull())
            {
                objInvokeResult.ResultCode = "TokenNull";
                objInvokeResult.ResultMessage = "注销失败，Token为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            objSSOProcess.LoginOut(token);
            objInvokeResult.ResultMessage = "注销成功";
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult Target(string token, string sign, string targetUrl)
        {
            if (targetUrl.IsNull())
            {
                return Http404();
            }
            targetUrl = targetUrl.DecodeUrl();

            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);

            if (!objSSOProcess.ValidTicket())
            {
                return Content("请求不合法");
            }

            UserInfo objUserInfo = objSSOProcess.GetUserInfo(token);
            if (objUserInfo == null)
            {
                return Redirect(objSSOProcess.CreateRedirectConvertUrl(targetUrl));
            }

            CookieHelper.SetCookieValue(token, CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");

            return Redirect(targetUrl);
        }

        public virtual ActionResult GetCurrentUserInfo()
        {
            UserInfo objUserInfo = CurrentBBSProcess.GetUserInfo();
            var json = new { UserName = objUserInfo.UserName, Avatar = objUserInfo.Avatar };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int? categoryId)
        {
            //todo:判断版块是否存在
            //return Content("123");
            //return Content(CurrentCommunityConfig.ConnectionString);
            return View();
        }
        public ActionResult UserArticles()
        {
            return View();
        }

        /// <summary>
        /// 文章详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Article(int? id)
        {
            if (!id.HasValue)
            {
                return Http404();
            }
            ArticleInfoProcess objArticleInfoProcess = new ArticleInfoProcess(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            ArticleResultInfo objArticleResultInfo = objArticleInfoProcess.GetArticleResultInfo(id.Value, false, false);
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                return Http404();
            }
            UserInfo objUserInfo = CurrentBBSProcess.GetUserInfo();
            if (objArticleResultInfo.ArticleInfo.IsRelease != 1 && !string.Equals(objArticleResultInfo.ArticleInfo.ObjectId, objUserInfo.UserId))
            {
                return Content("对不起，当前文章还在审核中");
            }
            //更新文章的浏览数
            objArticleInfoProcess.IncreaseViewCount(id.Value);

            return View();
        }

        public ActionResult Publish()
        {
            return View();
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult Thread(int? id)
        {
            if (!id.HasValue)
            {
                return Http404();
            }
            ArticleInfoProcess objArticleInfoProcess = new ArticleInfoProcess(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            ArticleResultInfo objArticleResultInfo = objArticleInfoProcess.GetArticleResultInfo(id.Value, false, false);
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                return Http404();
            }
            //更新文章的浏览数
            objArticleInfoProcess.IncreaseViewCount(id.Value);

            return View();
        }


        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult GetThread(int? id, int? isPictureThumbnail = 1, int? isContentThumbnail = 0)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!id.HasValue)
            {
                objInvokeResult.ResultCode = "IDEmpty";
                objInvokeResult.ResultMessage = "ID不存在";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            ArticleInfoProcess objArticleInfoProcess = new ArticleInfoProcess(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            ArticleResultInfo objArticleResultInfo = objArticleInfoProcess.GetArticleResultInfo(id.Value, true, true);
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                objInvokeResult.ResultCode = "ArticleEmpty";
                objInvokeResult.ResultMessage = "文章不存在";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            if (isPictureThumbnail.HasValue && isPictureThumbnail.Value == 1)
            {
                if (objArticleResultInfo.ArticlePictureCollection.IsNoNull())
                {
                    foreach (ArticlePicture objArticlePicture in objArticleResultInfo.ArticlePictureCollection)
                    {
                        //objArticlePicture.PictureUrl = Bigger.Qiniu.QiniuImageHelper.CreateThumbnailFromQiniu(objArticlePicture.PictureUrl, 50, 80);
                        objArticlePicture.PictureUrl = objArticlePicture.PictureUrl.CreateThumbnailByWidth(800);
                    }
                }
            }

            if (isContentThumbnail.HasValue && isContentThumbnail.Value == 1)
            {
                objArticleResultInfo.ArticleContent = objArticleResultInfo.ArticleContent.CreateThumbnailByWidthFromContent(800);
            }

            objInvokeResult.Data = objArticleResultInfo;

            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult HorizonList()
        {
            return View();
        }

        public ActionResult Horizon(int? id)
        {
            return Article(id);
        }
    }
}
